package com.smart.register.service.zookeeper;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

import com.smart.register.utils.RegisterDict;
import com.smart.register.utils.ResourcesUtils;

public class ZkClient {

	private static CuratorFramework client = null;
	
	private ZkClient(){}
	
	public synchronized static CuratorFramework getInstance(){
		if(client == null){
			generatorClient();
		}
		return client;
	}
	
	private synchronized static void generatorClient(){
		String times = ResourcesUtils.getValue(RegisterDict.zookeeper_sleep_times);
		String retries = ResourcesUtils.getValue(RegisterDict.zookeeper_retries);
		String server = ResourcesUtils.getValue(RegisterDict.zookeeper_server);
		RetryPolicy retryPolicy = new ExponentialBackoffRetry(Integer.parseInt(times), Integer.parseInt(retries));
		client = CuratorFrameworkFactory.newClient(server, retryPolicy);
		client.start();
	}
}
